﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TableParsers;

namespace SpeakupBackend.Stats
{
    [Serializable]
    public class LevelResultsEntry
    {
        public string Course;
        public int Level;
        public int? Grammar;
        public int? Vocabulary;
        public int? Listening;
        public int? Reading;
        public int Avarage;
    }

    [Serializable]
    public class LevelResultsTable: List<LevelResultsEntry>
    {
        public static LevelResultsTable Fetch(WebLayer.LoginToken loginToken)
        {
            WebPageParser.WebPageCache pageCache = new WebPageParser.WebPageCache();
            pageCache.LoginToken = loginToken;

            return Fetch(pageCache);
        }

        public static LevelResultsTable Fetch(WebPageParser.WebPageCache pageCache)
        {
            ResultsTable result = ResultsTable.Fetch(pageCache);
            return Create(result);
        }

        public static LevelResultsTable Create(ResultsTable table)
        {
            LevelResultsTable levelResultTable = new LevelResultsTable();

            foreach (var entry in table)
            {
                if(entry.IsLevel)
                {
                    LevelResultsEntry levelEntry = new LevelResultsEntry();
                    levelEntry.Course = entry.Course;
                    levelEntry.Level = entry.UnitOrLevel;
                    levelEntry.Grammar = entry.Grammar;
                    levelEntry.Vocabulary = entry.Vocabulary;
                    levelEntry.Listening = entry.Listening;
                    levelEntry.Reading = entry.Reading;
                    levelEntry.Avarage = entry.Avarage;

                    levelResultTable.Add(levelEntry);
                }
            }

            return levelResultTable;
        }
    }
}
